define(function(require, exports, module) {
	"use strict";

	var storageTools = require('StorageTools_Core');

	/*api配置*/
	exports.apiOption = {
		url: "http://api.agoshare.com"
	};

	/*获取api类型接口地址*/
	exports.getApiUrl = function(controller, action) {
		return exports.apiOption.url + "/" + controller + "/" + action;
	};

	/**
	 * 保存token
	 * @param {Object} token
	 */
	exports.saveToken = function(token) {
		storageTools.setStorageItem('token', token);
	}

	/**
	 * 保存user
	 * @param {Object} user
	 */
	exports.saveUser = function(user) {
		storageTools.setStorageItem('user', user);
	}

	/**
	 * 获取token
	 */
	exports.getToken = function() {
		return storageTools.getStorageItem('token');
	}

	/**
	 * 获取user
	 */
	exports.getUser = function() {
		return storageTools.getStorageItem('user');
	}

	/* get访问 */
	exports.get = function(url, data, success, error, options) {
		options = options || {};
		options.processData = true;
		options.headers = {
			'Content-Type': 'application/x-www-form-urlencoded',
			'ago-x-token': exports.getToken()
		};

		return doAjax(url, 'get', data,
			function(retval) {
				if(typeof(plus) != 'undefined') {
					plus.nativeUI.closeWaiting();
				}
				if(retval.Success) {
					success && success(retval.Result);
				} else {
					mui.toast(retval.Message, {
						duration: 'short',
						type: 'div'
					});
				}
			},
			function(retval) {
				if(typeof(plus) != 'undefined') {
					plus.nativeUI.closeWaiting();
				}
				mui.toast('server error', {
					duration: 'short',
					type: 'div'
				});
			}, options);
	};

	/* post访问 */
	exports.post = function(url, data, success, error, options) {
		options = options || {};
		options.processData = true;
		data = typeof data == 'object' ? JSON.stringify(data) : data;

		return doAjax(url, 'post', data,
			function(retval) {
				if(typeof(plus) != 'undefined') {
					plus.nativeUI.closeWaiting();
				}
				if(retval.Success) {
					success && success(retval.Result);
				} else {
					mui.toast(retval.Message, {
						duration: 'short',
						type: 'div'
					});
				}
			},
			function() {
				if(typeof(plus) != 'undefined') {
					plus.nativeUI.closeWaiting();
				}
				mui.toast('server error', {
					duration: 'short',
					type: 'div'
				});
				error && error();
			}, options);
	};

	/* 处理ajax请求，私有方法 */
	function doAjax(url, type, data, success, error, options) {
		var settings = {
			data: data,
			dataType: 'json', //服务器返回json格式数据
			type: type, //HTTP请求类型
			timeout: 10000, //超时时间设置为10秒；
			crossDomain: true,
			processData: false, //不處理data
			headers: {
				'Content-Type': 'application/json',
				'ago-x-token': exports.getToken()
			},
			success: success || function() {
				//成功处理
			},
			error: error || function(xhr, type, errorThrown) {
				//异常处理；
				console.log(xhr.response);
			}
		};
		settings = mui.extend(settings, options);
		return mui.ajax(url, settings);
	}

	/* 处理ajax请求，私有方法 */
	function newDoAjax(url, type, data, success, error, options) {
		var settings = {
			data: data,
			dataType: 'json', //服务器返回json格式数据
			type: type, //HTTP请求类型
			timeout: 10000, //超时时间设置为10秒；
			crossDomain: true,
			processData: false, //不處理data
			headers: {
				'Content-Type': 'application/x-www-form-urlencoded',
			},
			success: success || function() {
				//成功处理
			},
			error: error || function(xhr, type, errorThrown) {
				//异常处理；
				console.log(xhr.response);
			}
		};
		settings = mui.extend(settings, options);
		return mui.ajax(url, settings);
	}
});